<?php


/*
 * 重新初始化PHPMailer
 * */
function nicen_theme_mail_smtp( $phpmailer ) {
	global $desination_configs;


	/*
	 * 如果打开了smtp
	 * */
	if ( $desination_configs['document_smtp_open'] ) {
		$phpmailer->isSMTP();
		$phpmailer->SMTPAuth   = true;
		$phpmailer->CharSet    = "utf-8";
		$phpmailer->SMTPSecure = $desination_configs['document_smtp_protocol'];
		$phpmailer->Port       = $desination_configs['document_smtp_port'];
		$phpmailer->Host       = $desination_configs['document_smtp_server'];
		$phpmailer->Username   = $desination_configs['document_smtp_acccount'];
		$phpmailer->Password   = $desination_configs['document_smtp_password'];
		$phpmailer->setFrom( $desination_configs['document_smtp_acccount'], get_option( 'blogname' ) );
	}

}

add_action( 'phpmailer_init', 'nicen_theme_mail_smtp' );


/*
 * 记录发送日志
 * */
function nicen_theme_wp_mail_error( $wp_error ) {
	$file = get_template_directory() . '/common/log/smtp_fail.txt';//记录发送失败的信息
	if ( is_writable( $file ) ) {

		$text = date( "Y-m-d H:i:s", time() ) . '，' . implode( ",", $wp_error->get_error_messages() ) . "\n";

		return file_put_contents( $file, $text, FILE_APPEND );
	}


}


/*
 * 记录发送成功的日志
 * */
function nicen_theme_wp_mail_successed( $mail_data ) {
	$file = get_template_directory() . '/common/log/smtp_success.txt';//记录发送失败的信息
	if ( is_writable( $file ) ) {
		$text = date( "Y-m-d H:i:s", time() ) . '，给' . implode( ',', $mail_data['to'] ) . "发送成功！\n";

		return file_put_contents( $file, $text, FILE_APPEND );
	}
}


add_action( 'wp_mail_failed', 'nicen_theme_wp_mail_error', 10, 1 );
add_action( 'wp_mail_succeeded', 'nicen_theme_wp_mail_successed', 10, 1 );


function nicen_theme_comment_approved( $comment ) {
	global $desination_configs;
	if ( is_email( $comment->comment_author_email ) ) {
		$wp_email = $desination_configs['document_smtp_acccount'];
		$to       = trim( $comment->comment_author_email );

		$post_link = get_permalink( $comment->comment_post_ID );
		$subject   = '[通知]您的留言已经通过审核';
		$message   = '
            <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
            <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
                    <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
                        <h1 style="color:#fff; font-size:25px;line-height:30px; margin:0;"><a href="' . home_url() . '" style="text-decoration: none;color: #FFF;">' . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '</a></h1>
                    </div>
                <div style="padding:35px 40px 30px;">
                    <h2 style="font-size:18px;margin:5px 0;">您好，' . trim( $comment->comment_author ) . '：</h2>
                    <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您的留言已经通过了管理员的审核，摘要信息如下：</p>
                        <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
                            <thead>
                                <tr>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="280px;">文章</th>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="270px;">内容</th>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="110px;">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">《' . get_the_title( $comment->comment_post_ID ) . '》</td>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim( $comment->comment_content ) . '</td>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><a href="' . get_comment_link( $comment->comment_ID ) . '" style="color:#1E5494;text-decoration:none;vertical-align:middle;" target="_blank">查看留言</a></td>
                                </tr>
                            </tbody>
                        </table>
                        <br>
                    <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出，如果不是您本人操作，请忽略此邮件。</div>
                    <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
                        <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '</p>
                        <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;"><span style="border-bottom:1px dashed #ccc;" t="5" times="">' . wp_date( "Y年m月d日", time() ) . '</span></p>
                    </div>
                </div>
            </div>
        </div>';
		$from      = "From: \"" . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . "\" <$wp_email>";
		$headers   = "$from\nContent-Type: text/html; charset=" . get_option( 'blog_charset' ) . "\n";

		wp_mail( $to, $subject, $message, $headers );
	}
}

add_action( 'comment_unapproved_to_approved', 'nicen_theme_comment_approved' );

function nicen_theme_comment_notify( $comment_id ) {


	global $desination_configs;
	$comment        = get_comment( $comment_id );
	$parent_id      = $comment->comment_parent ? $comment->comment_parent : '';
	$spam_confirmed = $comment->comment_approved;


	if ( ( $parent_id != '' ) && ( $spam_confirmed == '1' ) ) {

		$wp_email = $desination_configs['document_smtp_acccount'];

		$to      = trim( get_comment( $parent_id )->comment_author_email );
		$subject = '[通知]您的留言有了新的回复';
		$message = '
            <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
            <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
                    <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
                        <h1 style="color:#fff; font-size:25px;line-height:30px; margin:0;"><a href="' . get_option( 'home' ) . '" style="text-decoration: none;color: #FFF;">' . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '</a></h1>
                    </div>
                <div style="padding:35px 40px 30px;">
                    <h2 style="font-size:18px;margin:5px 0;">您好' . trim( get_comment( $parent_id )->comment_author ) . '：</h2>
                    <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您的留言有了新的回复，摘要信息如下：</p>
                        <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
                            <thead>
                                <tr>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="235px;">原文</th>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="235px;">回复</th>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="100px;">作者</th>
                                    <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="90px;" >操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim( get_comment( $parent_id )->comment_content ) . '</td>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim( $comment->comment_content ) . '</td>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim( $comment->comment_author ) . '</td>
                                    <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><a href="' . get_comment_link( $comment->comment_ID ) . '" style="color:#1E5494;text-decoration:none;vertical-align:middle;" target="_blank">查看回复</a></td>
                                </tr>
                            </tbody>
                        </table>
                        <br>
                    <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出，如果不是您本人操作，请忽略此邮件。</div>
                    <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
                        <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '</p>
                        <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;"><span style="border-bottom:1px dashed #ccc;" t="5" times="">' . wp_date( "Y年m月d日", time() ) . '</span></p>
                    </div>
                </div>
            </div>
        </div>';
		$from    = "From: \"" . htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . "\" <$wp_email>";
		$headers = "$from\nContent-Type: text/html; charset=" . get_option( 'blog_charset' ) . "\n";
		wp_mail( $to, $subject, $message, $headers );
	}
}

add_action( 'comment_post', 'nicen_theme_comment_notify' );
add_action( 'comment_unapproved_to_approved', 'nicen_theme_comment_notify' );
